<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<!--驳回原因-->
<div class="modal fade" id="modal-create">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">驳回</h4>
            </div>
            <div class="modal-body">
                <form id="rejectForm" class="form-horizontal" role="form">
                    <input id="proId" type="hidden" th:value="${proJect.id}" name="proId">
                    <input id="createCode" type="hidden" name="createCode">
                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">驳回原因</label>
                        <div class="col-sm-10">
                            <textarea rows="3" class="form-control" name="rejectReason"
                                      placeholder="请输入驳回原因"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-reject">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header">
    <h1>审核产品结构</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <button th:if="${checkStatus == 0}" type="button" shiro:hasPermission="dsn:proCheck:check"
                            class="btn btn-success pull-right"
                            id="btn-check">同意
                    </button>
                    <button th:if="${checkStatus > 0}" type="button" shiro:hasPermission="dsn:proCheck:check"
                            class="btn btn-danger pull-right"
                            id="btn-cancel">撤销
                    </button>
                    <h4>项目信息</h4>
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>项目编号：</label><label th:text="${proJect.proCode}"></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>项目名称：</label><label th:text="${proJect.proName}"></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>数量：</label><label th:text="${proJect.proNum}"></label>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label>需方：</label><label th:text="${proJect.demander}"></label>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="form-inline">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label th:text="'分配科室：'+${proJect.depts}"></label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <h4>产品结构</h4>
                    <table id="bootstrap-table" style="width: 99.9%;"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    var table;

    /** 打开驳回原因模态框 */
    function openRejectReason(result) {
        $("#rejectForm").fillData(result);
        js.modal.open("modal-create");
        setTimeout(function () {
            js.validate.validing("rejectForm");
        }, 500);
    }

    $(function () {

        /** 关闭驳回模态框 */
        js.modal.hideEvent("modal-create", function () {
            $("#createCode").val("");
            $("#rejectReason").val("");
        });

        /** 驳回 */
        $("#btn-reject").click(function () {
            js.validSubmit({
                formId: "rejectForm",
                url: ctx + "designTask/proStruct/reject",
                data: new FormData($("#rejectForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-create");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

        /** 产品结构列表 */
        table = js.table.init({
            url: ctx + "designTask/proStruct/proStructCheckList",
            onLoadSuccess: function (data) {
                mergeCells(data, "createName", "operate", 1, $('#bootstrap-table')); //合并单元格
            },
            showExport: false,
            pagination: false,
            queryData: {proId: $("#proId").val()},
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "类型", field: 'special'},
                {title: "产品结构名称", field: 'proPart'},
                {title: "创建人", field: 'createName'},
                {title: "创建时间", field: 'createTime'},
                {
                    title: "状态", field: 'status',
                    formatter: function (value) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未提交</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-success">已提交</span>';
                        } else if (value === 3) {
                            return '<span class="badge label-success">已审核</span>';
                        }
                    }
                },
                {
                    title: "操作", field: 'operate', width: '110',
                    formatter: function (value, row) {
                        let actions = [];
                        if (hasP('dsn:proCheck:check') && row.status === 2) {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="openRejectReason(' + js.jsonStringify(row) + ')"> 驳回</a> ');
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /**
         * 合并单元格
         * @param data 原始数据（在服务端完成排序）
         * @param fieldName 合并依据属性名称
         * @param spanFieldName 合并属性名称
         * @param colspan 合并列
         * @param target 目标表格对象
         */
        function mergeCells(data, fieldName, spanFieldName, colspan, target) {
            //声明一个map计算相同属性值在data对象出现的次数和
            var sortMap = {};
            for (var i = 0; i < data.rows.length; i++) {
                for (var prop in data.rows[i]) {
                    if (prop == fieldName) {
                        var key = data.rows[i][prop]
                        if (sortMap.hasOwnProperty(key)) {
                            sortMap[key] = sortMap[key] * 1 + 1;
                        } else {
                            sortMap[key] = 1;
                        }
                        break;
                    }
                }
            }
            var index = 0;
            for (var prop in sortMap) {
                var count = sortMap[prop] * 1;
                $(target).bootstrapTable('mergeCells', {
                    index: index,
                    field: spanFieldName,
                    colspan: colspan,
                    rowspan: count
                });
                index += count;
            }
        }

        /** 审核通过 */
        $("#btn-check").click(function () {
            js.modal.confirm("确定同意该产品结构吗？", function (result) {
                js.post({
                    url: ctx + "designTask/proStruct/check",
                    data: {proId: $("#proId").val()},
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            location.href = ctx + "designTask/proStruct/checkProList";
                        } else {
                            js.modal.warning(result.msg);
                        }
                    }
                });
            })
        });

        /** 审核撤销 */
        $("#btn-cancel").click(function () {
            js.modal.confirm("确定撤销该产品结构吗？", function (result) {
                js.post({
                    url: ctx + "designTask/proStruct/cancel",
                    data: {proId: $("#proId").val()},
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            location.href = ctx + "designTask/proStruct/checkProList";
                        } else {
                            js.modal.warning(result.msg);
                        }
                    }
                });
            })
        });

    });


</script>
</body>
</html>
